<template>
  <el-container class="home_container">
    <!-- 头部区 -->
  <el-header>
    <div>
      <img src="../assets/shendun.jpg" alt="">
      <span>电商后台管理系统</span>
    </div>
    <el-button type="info" @click="loginOut">退出</el-button>
  </el-header>
  <!-- 页面主体区 -->
  <el-container>
    <!-- 侧边栏 -->
    <el-aside :width="collapseValue ? '64px' :'200px'">
      <div class="togger-button" @click="toggerBtn"
      :align="collapseValue ? 'center' :'center'">|||</div>
      <!-- active-text-color 选中颜色 -->
      <!-- router开始路由模式，可以打开其他的路径 -->
     <el-menu
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409EFF" unique-opened :collapse="collapseValue"
      :collapse-transition="false" router :default-active="activePath">
      <!-- 一级菜单 -->
      <!-- v-for循环要绑定一个唯一的key，:index绑定index是保证各打开各自的，互补干涉 -->
       <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
        <!-- 一级菜单模板区 -->
          <template slot="title">
          <!-- 图标 -->
           <i :class="icanObj[item.id]"></i>
          <!-- 文本 -->
           <span>{{item.authName}}</span>
          </template>
         <!-- 二级菜单 -->
          <!-- 保存状态 saveStatus 点击处于高亮状态-->
          <el-menu-item :index="'/' + it.path" v-for="it in item.children" :key="it.id"
          @click="saveStatus('/' + it.path)">
            <!-- 图标 -->
           <i class="el-icon-reading"></i>
            <!-- 文本 -->
           <span>{{it.authName}}</span>
          </el-menu-item>
       </el-submenu>
     </el-menu>
    </el-aside>
    <!-- 右侧内容区 -->
    <el-main>
      <!-- 路由占位符 -->
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      icanObj: {
        101: 'el-icon-goods',
        102: 'el-icon-shopping-cart-full',
        103: 'el-icon-setting',
        125: 'el-icon-user',
        145: 'el-icon-notebook-1'
      },
      // 是否折叠
      collapseValue: false,
      // 被激活的连接地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut () {
      window.sessionStorage.clear()
      this.$message.success('退出成功，再见！')
      this.$router.push('/login')
    },
    // 获取左侧菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('/menuList/query')
      // console.log(res)
      if (res.code !== '0') return this.$message.error('获取左侧菜单失败，原因：' + res.msg)
      this.menuList = res.data
    },
    toggerBtn () {
      this.collapseValue = !this.collapseValue
    },
    // 选中的高亮显示
    saveStatus (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home_container{
  height: 100%;
}
.el-header{
  // 左边贴边
  padding-left: 0;
  // 右侧退出按钮上下居中
  align-items: center;
  background-color: #373d41;
  display: flex;
  // 左右贴边对齐
  justify-content: space-between;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span{
      margin-left: 10px;
    }
  }
  img{
    max-height: 60px;
  }
}
.el-aside{
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: #eaedf1;
}
.togger-button{
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  // div文本居中
  // text-align: center;
  // 字体间距
  letter-spacing: 0.2em;
  // 鼠标放上去有小手
  cursor: pointer;
}
</style>
